package com.patui.Ui;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.patui.Logic.FrameSevice;
import com.patui.Weibo.AccessToken;
import com.patui.Weibo.Weibo;
public class LoginWebViewActivity extends Activity {
	private  WebView mWebView;
	private final String LOGTAG = "AuthorizationAct";
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    FrameSevice.addActivity(LoginWebViewActivity.this);
	    String url = getIntent().getStringExtra("loadUrl");
	    System.out.println("url+"+url);
	    mWebView=new WebView(this);
	    mWebView.loadUrl(url);
		setContentView(mWebView);
		mWebView.clearCache(true);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setSupportZoom(true);
		mWebView.getSettings().setBuiltInZoomControls(true);
		mWebView.setWebViewClient(new WebViewC()); 
		
    }
	
	class WebViewC extends WebViewClient{
		private int index = 0;
		@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
        }
		public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
			 handler.proceed() ;
		 }

		@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
	        super.onPageStarted(view, url, favicon);
	        if( url.contains(Weibo.callBackUrl) && index == 0){
	        	index ++;
	        	Weibo mWeibo=Weibo.getInstance();
	        	mWeibo.setAccessToken(new AccessToken(url));
	        	AccessToken m=mWeibo.getAccessToken();
	        	System.out.println("getToken--"+m.getToken()+"getSecret()--"+m.getSecret());
	    		Intent myIntent = new Intent();
	    		myIntent.setAction("com.patui.Ui.LoginActivity");
	    		sendBroadcast(myIntent);
            	LoginWebViewActivity.this.finish();
           }
	       
        }

		@Override
        public void onPageFinished(WebView view, String url) {
	        // TODO Auto-generated method stub
	        super.onPageFinished(view, url);
        }
	}
}
